import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.14
import QtQuick.Dialogs 1.2
import MyModule 1.0

Window {
    id:root
    visible: true
    width: 920
    height: 640
    objectName:"rootw"
    Component.onCompleted: {

        player.mySignal.connect(handleSignal)
        player.setWH(player.width, player.height)
    }

    function handleSignal(count)
    {
        repeat.model=count

        for(let i = 0;i < count;i++){

            var v = player.getText(i)
            repeat.itemAt(i).text=v
            console.log("text3:", v)
        }
    }

    Rectangle{
        id:toolBar
        width: parent.width-10
        x:parent.x
        height: 30

        Button{
            text:"打开图片"
            width:70
            height: parent.height

            onClicked: {
                console.log("open")
                openPic.open()
            }
        }
    }

    FileDialog {
        id: openPic
        title: qsTr("选择图片文件")
        selectExisting: true
        selectFolder: false
        selectMultiple: false
        nameFilters: [qsTr("Video files(*.jpg *.png *.jpeg)"),qsTr("All files(*)")]
        onAccepted: {
            console.log("You chose: " + openPic.fileUrl.toString().substring(8,openPic.fileUrl.length))
            player.setImage(openPic.fileUrl.toString().substring(8,openPic.fileUrl.length))
        }
        onRejected: {
            console.log("Canceled");
        }
    }

    PlayerItem {
        id:player
        width: parent.width/2
        height: parent.height-toolBar.height
        anchors.top:toolBar.bottom
        fillColor:"black"
        property var prevHl:-1
        MouseArea {
        id: mouseArea
        anchors.fill: parent
        onClicked: {
            var x = mouse.x;
            var y = mouse.y;
            var n = player.mousePos(x, y,player.width, player.height); // 调用 Python 端的槽函数
            console.log("ret:", n)
            if(player.prevHl != -1)
            {
                 repeat.itemAt(player.prevHl).color="black"
            }
            repeat.itemAt(n).color="red"
            player.prevHl = n
        }
    }
    }

    Column {
        anchors.left:player.right
        anchors.top:player.top
        width:parent.width/2
        height:player.height
        property int labelCount: 0

        Repeater {
            id:repeat
            model: parent.labelCount
            delegate: Label {
                height:30
                text: ""
                font.family: "微软雅黑"
                font.pixelSize: 14
                background: Rectangle {
                    id:repeatBg
                    //color: "red"
                    border.color: "#ddd"
                }
            }
        }
    }
}

